package busca.tesoros.uclm;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import org.json.JSONObject;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.content.*;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class PistaActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	
	public static final String SERVERIP = "192.168.1.6";
	public static final int SERVERPORT = 8080;
	
	private Intent i=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pistafoto);
        
        View botonIrCamara = findViewById(R.id.botonircamara);
        botonIrCamara.setOnClickListener(this);
                
        String inTxt = "";
        String response="";
        String descripcion="";
        String tipo="";
		try {
			Log.i("Client", "Conectting to "+ SERVERIP + ":" + SERVERPORT);
			InetAddress serverAddr = InetAddress.getByName(SERVERIP);
    		Socket clientSocket = new Socket(serverAddr, SERVERPORT);
			
			if(clientSocket.isConnected()) {
				
				PrintWriter out = new PrintWriter(
					new BufferedWriter(new OutputStreamWriter(clientSocket
						.getOutputStream())), true);
				
				//pide la siguiente pista
				out.println("{operacion:pista, numero:1}");

				BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()),1024);

				        			
				Log.i("Client", "Response from server:\n");
				String line;
				
				while ((line = in.readLine()) != null) {
					Log.i("Client", line);
					inTxt += line;
				}
				
				JSONObject inJson = new JSONObject(inTxt);
				response= inJson.getString("response");
				tipo= inJson.getString("tipo");
				
				if(response.equals("fin"))
				{
					 i = new Intent(this, SelMisionActivity.class);
					 findViewById(R.id.botonircamara).setEnabled(true);
				}
				else
				{
					findViewById(R.id.botonircamara).setEnabled(true);				
					if(tipo.equals("foto"))
					{
						i = new Intent(this, CodigosQRActivity.class);
					}
					else if(tipo.equals("posicion"))
					{
						 i= new Intent(this, LocalizacionActivity.class);
					}
					else if(tipo.equals("usuario"))
					{
						i= new Intent(this, CodigosQRActivity.class);
					}
					 
				}
	    		
	    		descripcion=inJson.getString("descripcion");
				Log.i("Client", "-----------------\n");
				
				 
			}
			clientSocket.close();
		}
		catch (Exception e) {
			Log.e("Client", "Error connecting to the library server.", e);
		}
        
		((TextView)findViewById(R.id.Titulo)).setText("Captura "+tipo);
   	 ((TextView)findViewById(R.id.textoPista)).setText(descripcion);
   	 
   	 
   	 ImageView imgResult= (ImageView)findViewById(R.id.imagenEjemplo);
   	if(tipo.equals("foto")){
   		imgResult.setImageResource(R.drawable.foto);
   	    imgResult.setVisibility( View.VISIBLE );
    }
    else if(tipo.equals("usuario"))
    		{
    	imgResult.setImageResource(R.drawable.user);
   	    imgResult.setVisibility( View.VISIBLE );
    		}
    else if(tipo.equals("posicion"))
    		{
    	imgResult.setImageResource(R.drawable.gps);
   	    imgResult.setVisibility( View.VISIBLE );
   	    }
    }
   	
   	
	//   Control Menu
	//
	private static final int MNU_OPC1 = 1;
	private static final int MNU_OPC2 = 2;
	private static final int MNU_OPC3 = 3;
	
	private static final int SMNU_OPC1 = 31;
	private static final int SMNU_OPC2 = 32;
	Boolean star=false;
	private TextView lblMensaje;
	MediaPlayer mp;
				
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent qr;
    	
        switch (item.getItemId()) {
        
            case R.id.menuimagen:
            	qr = new Intent(this,MostrarQRActivity.class);
          		startActivity(qr);		             
          		 return true;
          		               
            case R.id.menusalir:
            	qr = new Intent(this, FinActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            	startActivity(qr);
            	finish();
            	 return true;
            	 
            case R.id.menusonido:
            	if(star)
            	{
            	mp.stop();
            	star=false;
            	}
            	else
            	{		            		
                    mp = MediaPlayer.create(this, R.raw.melodia_de_fondo);
	            	mp.setLooping(true);
            		mp.start();
            		star=true;
            	}
            	return true;
		default:
                return super.onOptionsItemSelected(item);
        }
    }
  
    
    public void onClick(View v) {
    	switch (v.getId()) {
    	case R.id.botonircamara:    		
    		startActivity(i);
    		finish();
    		break;   
	}
    	
    }
    
}

    	
        
      
        


